<?php

namespace App\Http\Servers\Admin;

class ClearServer
{
    private $imageServer;

    public function __construct(ImageServer $imageServer)
    {
        $this->imageServer = $imageServer;
    }

    //清除无用图片
    public function delImage()
    {
        //清除无用图片
        $data['imageNum'] = \App\Image::count();

        //banner图片的id
        $bannerImageIds = \App\Banner::lists('image')->toArray();
        //得到友情链接图片的id
        $friendlyLinkImageIds = \App\FriendlyLink::lists('image')->toArray();
        //得到模块信息图片的id
        $infoImageIds = \App\InfoImageLines::lists('image')->toArray();
        //得到模块分类图片的id
        $sortImageIds = \App\Sort::lists('image')->toArray();
        //得到企业信息图片的id
        $pageInfoImageIds = \App\PageInfo::lists('image')->toArray();
        //得到企业模块图片的id
        $pageLabelImageIds = \App\PageLabel::lists('image')->toArray();
        //得到网站logo图片的id
        $web_log[] = (int)\App\Set::where('name', 'web_logo')->value('value');
        //得到网站二维码图片的id
        $web_qrcode[] = (int)\App\Set::where('name', 'web_qrcode')->value('value');
//        dd($bannerImageIds, $friendlyLinkImageIds, $sortImageIds, $infoImageIds, $pageInfoImageIds, $pageLabelImageIds, $web_log, $web_qrcode);
        $tmpImageIds = array_unique(array_collapse([$bannerImageIds, $friendlyLinkImageIds, $sortImageIds, $infoImageIds, $pageInfoImageIds, $pageLabelImageIds, $web_log, $web_qrcode]));
        sort($tmpImageIds);
        foreach ($tmpImageIds as $id) {
            $id && $imageIds[] = $id;
        }

        //得到要清除的图片的id
        $delImages = \App\Image::whereNotIn('id', $imageIds)->lists('id');
        $data['delImageNum'] = count($delImages);

        if ($data['delImageNum'] > 0) {
            foreach ($delImages as $id) {
                $this->imageServer->deleteImage($id);
                \App\InfoImageLines::where('image', $id)->delete();
            }
        }

        return message(true, '清理数据成功', '共有 ' . $data['imageNum'] . ' 张图片,其中 ' . $data['delImageNum'] . ' 张无用图片已清除!');
    }

    //清除缓存文件
    public function delFile()
    {
        //清除缓存文件
        foreach (config('app.clear_file') as $path) {
            $delFile[] = glob($path);
        }

        $delFiles = array_collapse($delFile);

        $data['delFileNum'] = count($delFiles);
        foreach ($delFiles as $file) {
            unlink($file);
        }
        return message(true, '清理数据成功', '清除 ' . $data['delFileNum'] . ' 个页面缓存文件!');
    }

    //清空回收站
    public function delTrash()
    {
        $infos = \App\Info::onlyTrashed()->get();
        $count = count($infos);
        foreach ($infos as $info) {
            $info->forceDelete();
            \App\InfoSortLines::where('info', $info['id'])->delete();
            \App\InfoImageLines::where('info', $info['id'])->delete();
        }
        return message(true, '清空回收站成功', '清除 ' . $count . ' 个回收站信息!');
    }

    //清理填充信息
    public function delFill()
    {
        $infos = \App\Info::get();
        $count = count($infos);
        \App\Info::where('id', '>', 0)->delete();
        return message(true, '清理填充数据成功,填充数据进入回收站,您可以选择彻底清除或者恢复', '清除 ' . $count . ' 个信息!');
    }

    //清理在线留言
    public function delMessage()
    {
        $messages = \App\Message::get();
        $count = count($messages);
        \App\Message::where('id', '>', 0)->delete();
        return message(true,'清理在线留言数据成功', '清理在线留言数据' . $count . '条!');
    }

    //设置debug
    public function setDebug()
    {
        $set = file_get_contents('.env');
        $newset = str_replace('APP_DEBUG=true', 'APP_DEBUG=false', $set);
        $status = '关闭';
        if ($newset === $set) {
            $newset = str_replace('APP_DEBUG=false', 'APP_DEBUG=true', $set);
            $status = '开启';
        }

        file_put_contents('.env', $newset);

        return message(true, '设置debug成功', '当前debug状态:' . $status);
    }
}